package Unix::RatfinderIO;
use strict;
use Term::Complete;
use Exporter;
our @ISA = 'Exporter';

# will save memory.
our %EXPORT_TAGS = ( 'all' => [ qw( ) ] ); 
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
our @EXPORT = qw( echo prompt menuprog);
our $VERSION = '0.01';

sub echo {#{{{
	if (defined$ENV{'RATPOISON'})
	{
		system("$ENV{'RATPOISON'} -c \"echo @_\"");
		print "desde echo @_\n";
	}elsif ($ENV{DISPLAY}) {
		my $README;
		open($README, "zenity --info --text=\"@_\" |") or die("Couldn't fork: $|\n");
		close($README);
	} else { print @_ ; }
	return ;
}	# ----------  end of subroutine rpecho  ----------#}}}

#===  FUNCTION  ================================================================
#         NAME:  prompt
#      PURPOSE:  ????
#   PARAMETERS:  text, hasref to completions
#      RETURNS:  ????
#  DESCRIPTION:  ????
#       THROWS:  no exceptions
#     COMMENTS:  none
#     SEE ALSO:  n/a
#===============================================================================
sub prompt {#{{{
	my $text = $_[0];
	if ($ENV{DISPLAY} && defined$ENV{'RATPOISON'})
	{
		$_ = `$ENV{'RATPOISON'} -c \"prompt $text\"`;
		chomp;
		return $_;
	}elsif ($ENV{DISPLAY}) {
		my $README;
		open($README, "zenity --entry --text=\"$text\" |") or die("Couldn't fork: $|\n");
		my $res;
		while (<$README>)
		{
			$res = $_;
		}
		close($README);

		if ($? != 0) { # $? es -1 si no se pudo ejecutar
			return "(null)";
		}
		chomp ($res);
		return $res;
	} else {                                    # no X
		return &_Term;
	}
}	# ----------  end of subroutine rpprompt  ----------#}}}

sub _Term {
#	print "@_\n";
	#while (Complete $_[0] ,qw/fdsa dsaf/){
	$_ = Complete $_[0] , (keys %{$_[1]},qw(quit gen vi ff)) ;
	return '(null)' if (/quit/);
	return $_;
}

sub menuprog {#{{{
	# TODO : enable menuexes outside path
#	print ((split / / ,$_[0])[0]), "\n";
#	print join (' ', @_), "\n";
	return join(' ', @_) if ( ( 0 == system("which ". ((split / / ,$_[0])[0])) ) || (-x $_[0]) );
	return 	"zenity --title 'filelist' --list --print-column 2 --hide-column 2  --column 'name' --column 'value' " if (0 == system("which zenity"));
	echo("please, set your menuexe");
	return "ratmen "; 
}	# ----------  end of subroutine menuprog  ----------#}}}

1;
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker : ##
